const {claimsModel} = require('./models/claimsModel')

module.exports.addClaims = async (params) => {
  const data = await claimsModel.create(params);
  return data;
}

//获取所有理赔信息
module.exports.getClaims = async ({pageSize, currentPage, state}) => {
  const claims = await claimsModel.find({
    delete: false,
  })
  // .populate('ordersId, salemenId')
  .limit(pageSize - 0)
  .skip((currentPage - 1) * pageSize)
  //获取数据的总条数
  const total = await claimsModel.countDocuments({
    delete: false
  })
  // //获取总页数
  const pages =  Math.ceil(total / pageSize);
  let data = {claims, total, pages}
  return data;
  
}

//根据state获取
module.exports.getClaimsByState = async ({pageSize, currentPage, state}) => {

  const claims = await claimsModel.find({
    delete: false,
    state: state
  })
  // .populate('ordersId, salemenId')
  .limit(pageSize - 0)
  .skip((currentPage - 1) * pageSize)
  //获取数据的总条数
  const total = await claimsModel.countDocuments({
    delete: false,
    state: 1
  })
  // //获取总页数
  const pages =  Math.ceil(total / pageSize);
  let data = {claims, total, pages}
  return data;
}

//根据客户获取所有理赔信息
module.exports.getClaimsById = async (params) => {
  const data = await claimsModel.find(params);
  return data;
}

//根据销售员获取所有理赔信息
module.exports.getClaimsBySalesMen = async ([cond, pageData]) => {    //[{销售员_id, state: 1, delete: false}, pageData]    [cond, pageData]
  const claims = await claimsModel.find(cond).limit(pageData.pageSize - 0).skip((pageData.currentPage - 1) * pageData.pageSize)
  const total = await claimsModel.countDocuments(cond)
  const pages = Math.veil(total / pageData.pageSize)
  let data = {claims, total, pages}
  console.log(data);
  return data;

}


//修改订单
module.exports.changeClaims = async ([oldClaim, newClaim]) => {
  const data = await claimsModel.updateOne(oldClaim, newClaim)
  return data
}

//删除订单
module.exports.deleteClaims = async (params) => {
  const data = await claimsModel.deleteOne(params);
  return data;
}

//搜索
module.exports.searchClaims = async ({pageSize, currentPage, type='name', search=''}) => {
  console.log(type, search);
  const claims = await claimsModel.find({
    delete: false,
    [type]: { $regex: search, $options: '$i' }
  })
  // .populate('ordersId, salemenId')
  .limit(pageSize - 0)
  .skip((currentPage - 1) * pageSize)
  //获取数据的总条数
  const total = await claimsModel.countDocuments({
    delete: false
  })
  // //获取总页数
  const pages =  Math.ceil(total / pageSize);
  let data = {claims, total, pages}
  return data;
  
}